<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">

      <el-form-item label="采购员id" prop="assigneeId">
        <el-input v-model="dataForm.assigneeId" placeholder="采购单id"></el-input>
      </el-form-item>
      <el-form-item label="采购员名字" prop="assigneeName">
        <el-input v-model="dataForm.assigneeName" placeholder="采购单名字"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input v-model="dataForm.priority" placeholder="优先级"></el-input>
      </el-form-item>
      <el-form-item label="采购单状态" prop="status">
<!--        [0新建，1已分配，2正在采购，3已完成，4采购失败]-->
<!--        <el-input v-model="dataForm.status" placeholder="采购单状态[0新建，1已分配，2正在采购，3已完成，4采购失败]"></el-input>-->
        <purchase-status-component @update-purchase-status="updatePurchaseStatus" :status="dataForm.status"></purchase-status-component>
      </el-form-item>
      <el-form-item label="仓库id" prop="wareId">
<!--        <el-input v-model="dataForm.wareId" placeholder="仓库id"></el-input>-->
        <select-ware-by-name-component @update-ware-id="updateWareId" :wareId="dataForm.wareId"></select-ware-by-name-component>
      </el-form-item>
      <el-form-item label="采购数量" prop="amount">
        <el-input v-model="dataForm.amount" placeholder="采购数量"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间（系统自动更新）" disabled></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间（系统自动更新）" disabled></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  components:{
    'select-ware-by-name-component': ()=> import('../wareInfoComponent/select-ware-by-name-component'),
    'purchase-status-component':()=> import('./purchaseStatusComponent')
  },
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        assigneeId: '',
        assigneeName: '',
        phone: '',
        priority: '',
        status: '',
        wareId: '',
        amount: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        assigneeId: [
          {required: true, message: '采购员id不能为空', trigger: 'blur'}
        ],
        assigneeName: [
          {required: true, message: '采购单名字不能为空', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '电话号码不能为空', trigger: 'blur'}
        ],
        priority: [
          {required: true, message: '优先级不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '采购单状态[0新建，1已分配，2正在采购，3已完成，4采购失败]不能为空', trigger: 'blur'}
        ],
        wareId: [
          {required: true, message: '仓库id不能为空', trigger: 'blur'}
        ],
        amount: [
          {required: true, message: '采购数量不能为空', trigger: 'blur'}
        ],
        /*createTime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],
        updateTime: [
          {required: true, message: '更新时间不能为空', trigger: 'blur'}
        ]*/
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/wms/purchase/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.assigneeId = data.purchase.assigneeId
              this.dataForm.assigneeName = data.purchase.assigneeName
              this.dataForm.phone = data.purchase.phone
              this.dataForm.priority = data.purchase.priority
              this.dataForm.status = data.purchase.status
              this.dataForm.wareId = data.purchase.wareId
              this.dataForm.amount = data.purchase.amount
              this.dataForm.createTime = data.purchase.createTime
              this.dataForm.updateTime = data.purchase.updateTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/wms/purchase/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'assigneeId': this.dataForm.assigneeId,
              'assigneeName': this.dataForm.assigneeName,
              'phone': this.dataForm.phone,
              'priority': this.dataForm.priority,
              'status': this.dataForm.status,
              'wareId': this.dataForm.wareId,
              'amount': this.dataForm.amount,
              'createTime': this.dataForm.createTime,
              'updateTime': this.dataForm.updateTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },

    updateWareId(wareId){
      this.dataForm.wareId = wareId;
    },

    updatePurchaseStatus(status){
      this.dataForm.status = status;
    }
  }
}
</script>
